package vip.zhenzicheng.algorithm.sort;

import java.util.Arrays;

import static vip.zhenzicheng.algorithm.util.ArrayUtils.swap;

/**
 * 冒泡排序
 *
 * @author zhenzicheng
 * @date: 2022-05-17 08:34
 */
public class BubbleSort {

  public static void main(String[] args) {
    int[] arr = {1, 7, 2, 8, 9, 2, 4, 6, 12, 2, 43, 75, 4};

    bubbleSort(arr);
    System.out.println(Arrays.toString(arr));
  }

  public static void bubbleSort(int[] arr) {
    if (arr == null || arr.length < 2) {
      return;
    }
    int N = arr.length;
    for (int end = N - 1; end >= 0; end--) {
      for (int second = 1; second <= end; second++) {
        if (arr[second - 1] > arr[second]) {
          swap(arr, second - 1, second);
        }
      }
    }
  }

}
